/*
 * 文件名：RunLogImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-28
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.log.syslog.imp;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.wootide.cms.log.syslog.RunLog;

/**
 * 系统运行日志实现层
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 * @author ozm
 * @version 2013-1-28
 * @see RunLogImp
 * @since 2013-1-28上午10:13:50
 */
public class RunLogImp implements RunLog
{
    
    /**
     *  
     * Description: Error级别日志 带异常信息
     * Implement: <br>
     * @param detail   详细描述
     * @param t        异常
     * @see
     * @return void
     * @author ozm
     */
    public void error(Object detail, Throwable t)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.error(buffer.append(detail).toString(), t);
        
    }
    
    /**
     * 
     * Description: Error级别日志
     * Implement: <br>
     * @param detail 详细描述
     * @see
     * @return void
     * @author ozm
     */
    public void error(Object detail)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.error(buffer.append(detail).toString());
        
    }
    
    /**
     * 
     * Description: Error级别日志
     * Implement: <br>
     * @param obj obj
     * @param request  HttpServletRequest
     * @see
     * @return void
     * @author ozm
     */
    public void error(Object obj, HttpServletRequest request)
    {
        
        if (null == request)
        {
            error(obj);
            return;
        }
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
//        String ip = request.getRemoteAddr();

//        // 获得操作员信息
//        SysOperator sysOperator = (SysOperator) request.getSession()
//                .getAttribute("sysOperator");
//
//        buffer.append("Access IP:" + ip).append(
//                " Operator:" + sysOperator.getFullName()
//                        + " Operate  Infomation:");
        logger.error(buffer.append(obj).toString());
    }
    
    /**
    *
    * Description: 打印error运行日志
    * Implement: <br>
    * @param obj  obj
    * @param t     异常
    * @param request HttpServletRequest
    * @see
    * @return void
    * @author ozm
    */
    public void error(Object obj, Throwable t, HttpServletRequest request)
    {
        if (null == request)
        {
            error(obj, t);
            return;
        }
        Logger logger = getLogger();

        StringBuilder buffer = createMethodNameLog();
        // 获得请求IP
//        String ip = request.getRemoteAddr();
//
//        // 获得操作员信息
//        SysOperator sysOperator = (SysOperator) request.getSession()
//                .getAttribute("sysOperator");
//
//        buffer.append("Access IP:" + ip).append(
//                " Operator:" + sysOperator.getFullName()
//                        + "   Operate Infomation:");
        logger.error(buffer.append(obj).toString(), t);
        
    }
    
    /**
     * 
     * Description: debug级别日志 带异常信息
     * Implement: <br>
     * @param detail 详细描述
     * @param t  异常
     * @see
     * @return void
     * @author ozm
     */
    public void debug(Object detail, Throwable t)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.debug(buffer.append(detail).toString(), t);
        
    }
    
    /**
     * 
     * Description: debug级别日志
     * Implement: <br>
     * @param detail 详细描述
     * @see
     * @return void
     * @author ozm2013-1-28上午10:04:44
     */
    public void debug(Object detail)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.debug(buffer.append(detail).toString());
        
    }
    
    /**
     * 
     * Description: info级别日志
     * Implement: <br>
     * @param detail 详细描述
     * @see
     * @return void
     * @author ozm2013-1-28上午10:06:30
     */
    public void info(Object detail)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.info(buffer.append(detail).toString());
        
    }
    
    /**
     * 
     * Description: info级别日志 带异常信息
     * Implement: <br>
     * @param detail 详细描述
     * @param t 异常
     * @see
     * @return void
     * @author ozm2013-1-28上午10:06:56
     */
    public void info(Object detail, Throwable t)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.info(buffer.append(detail).toString(), t);
        
    }
    
    /**
     * 
     * Description: info级别日志 带异常信息
     * Implement: <br>
     * @param obj  obj
     * @param request  HttpServletRequest
     * @see
     * @return void
     * @author ozm2013-1-28上午10:08:21
     */
    public void info(Object obj, HttpServletRequest request)
    {
        if (null == request)
        {
            info(obj);
            return;
        }
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        // 获得请求IP
//        String ip = request.getRemoteAddr();
//        // 获得操作员信息
//        SysOperator sysOperator = (SysOperator) request.getSession()
//                .getAttribute("sysOperator");
//        buffer.append("Access IP:" + ip).append(
//                " Operator:" + sysOperator.getFullName()
//                        + " Operate  Infomation:");
        logger.info(buffer.append(obj).toString());
        
    }
    
    /**
     * 
     * Description: fatal级别日志 带异常信息
     * Implement: <br>
     * @param detail 详细描述
     * @param t  异常
     * @see
     * @return void
     * @author ozm2013-1-28上午10:09:02
     */
    public void fatal(Object detail, Throwable t)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.error(buffer.append(detail).toString(), t);
        
    }
    
    /**
     * 
     * Description: fatal级别日志
     * Implement: <br>
     * @param detail  详细描述
     * @see 
     * @return void
     * @author ozm2013-1-28上午10:09:31
     */
    public void fatal(Object detail)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.warn(buffer.append(detail).toString());
        
    }
    
    /**
     * 
     * Description:warn级别日志 带异常信息
     * Implement: <br>
     * @param detail 详细描述
     * @param t 异常
     * @see
     * @return void
     * @author ozm2013-1-28上午10:09:55
     */
    public void warn(Object detail, Throwable t)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.error(buffer.append(detail).toString(), t);
        
    }
    
    /**
     * 
     * Description: warn级别日志
     * Implement: <br>
     * @param detail  详细描述
     * @see
     * @return void
     * @author ozm2013-1-28上午10:10:18
     */
    public void warn(Object detail)
    {
        Logger logger = getLogger();
        StringBuilder buffer = createMethodNameLog();
        logger.warn(buffer.append(detail).toString());
        
    }
    
    /**
     * 
     * Description: 判断是否是Info级别
     * Implement: <br>
     * @see
     * @return boolean 
     * @author ozm2013-1-28上午10:10:39
     */
    public boolean isInfo()
    {
        Logger logger = getLogger();
        return logger.isInfoEnabled();
    }
    
    /**
     * 
     * Description: 判断是否是Debug级别
     * Implement: <br>
     * @see
     * @return boolean
     * @author ozm2013-1-28上午10:11:21
     */
    public boolean isDebug()
    {
        Logger logger = getLogger();
        return logger.isDebugEnabled();
    }
    
    /**
     * 
     * Description: 判断是否是Error级别
     * Implement: <br>
     * @see
     * @return boolean
     * @author ozm2013-1-28上午10:11:40
     */
    public boolean isError()
    {
        Logger logger = getLogger();
        return logger.isEnabledFor(Level.ERROR);
    }
    
    /**
     * 
     * Description: 判断是否是Warn级别
     * Implement: <br>
     * @see
     * @return boolean
     * @author ozm2013-1-28上午10:12:04
     */
    public boolean isWarn()
    {
        Logger logger = getLogger();
        return logger.isEnabledFor(Level.WARN);
    }
    
    /**
     * 
     * Description: 判断是否是Fatal级别
     * Implement: <br>
     * @see
     * @return boolean
     * @author ozm2013-1-28上午10:12:27
     */
    public boolean isFatal()
    {
        Logger logger = getLogger();
        return logger.isEnabledFor(Level.FATAL);
    }
    
    /**
     * 
     * Description: 获取log4j日志记录器，动态加载日志器名
     * Implement: <br>
     * @see
     * @return Logger 日志记录器实例
     * @author ozm2013-1-28上午10:24:29
     */
    private Logger getLogger()
    {
        // 第二个堆栈
        final int iStackTrace = 2;

        Throwable throwable = new Throwable();
        StackTraceElement[] stackTraceElement = throwable.getStackTrace();
        String className = stackTraceElement[iStackTrace].getClassName();
        Logger logger = Logger.getLogger(className);

//        //动态设置系统日志级别
//        if (null != LongLevel.getLevel())
//        {
//            logger.setLevel(LongLevel.getLevel());
//        }
//       
        return logger;
    }

    /**
     * 
     * Description: 获取包含方法名称的日志StringBuilder
     * Implement: <br>
     * @see
     * @return StringBuilder 
     * @author ozm2013-1-28上午10:58:58
     */
    private StringBuilder createMethodNameLog()
    {
        String methodName = getMethodName();
        StringBuilder buffer = new StringBuilder();
        buffer.append("[").append(methodName).append("()] ");
        return buffer;
    }

    /**
     * 
     * Description: 获取方法名称
     * Implement: <br>
     * @see
     * @return String
     * @author ozm2013-1-28上午11:00:09
     */
    private String getMethodName()
    {
        // 第三个堆栈
        final int iStackTrace = 3;
        Throwable throwable = new Throwable();
        StackTraceElement[] stackTraceElement = throwable.getStackTrace();
        String methodName = stackTraceElement[iStackTrace].getMethodName();
        return methodName;
    }
    
}
